home *** CD-ROM | disk | FTP | other *** search
- /*
- * Prefs.c - Handle preferences file
- */
-
- # include <GestaltEqu.h>
- # include <Folders.h>
- # include <Script.h>
-
- # include "TransSkel.h"
-
- # include "C-ABC.h"
- # if ENABLE_DEBUG
- # include "Debug.h"
- # endif
-
-
- static OSType
- MakePrefsFileFSS (FSSpec *fss)
- {
- long result;
- short vRefNum;
- long dirID;
- OSErr err;
-
- if (!SkelQuery (skelQHasGestalt)
- || Gestalt (gestaltFindFolderAttr, &result) != noErr
- || ((1 << gestaltFindFolderPresent) & result) == 0)
- return (gestaltUnknownErr);
- err = FindFolder (kOnSystemDisk, kPreferencesFolderType,
- kDontCreateFolder, &vRefNum, &dirID);
- if (err == noErr)
- err = FSMakeFSSpec (vRefNum, dirID, prefsFileName, fss);
- return (err);
- }
-
-
- static void
- InterpPreferences (Handle h, long size, Prefs *prefs)
- {
- Boolean result = false; /* assume the worst */
-
- switch (* (short *) *h) /* switch on version number */
- {
- case 2: /* only version 2 is used currently */
- if (size == sizeof (Prefs))
- {
- *prefs = * (Prefs *) *h;
- result = true;
- }
- break;
- }
- if (!result) /* data malformed or unknown version */
- {
- # if ENABLE_DEBUG
- DisplayCString ("prefs file contents are malformed.\r");
- # endif
- }
- }
-
-
- /*
- * Read preferences file to get preferences values.
- * The preferences structure should be initialized by the caller.
- * If the preferences file can't be read or is malformed, the structure
- * will be undisturbed; otherwise the values in the file override the
- * defaults.
- */
-
- void
- ReadPreferences (Prefs *prefs)
- {
- FSSpec fss;
- short vRefNum;
- long size, count;
- Handle h;
- OSErr err;
-
- if ((err = MakePrefsFileFSS (&fss)) != noErr)
- {
- # if ENABLE_DEBUG
- DisplayCString ("No Preferences file, err = ");
- DisplayLong (err);
- DisplayLn ();
- # endif
- return;
- }
- if (FSpOpenDF (&fss, fsRdPerm, &vRefNum) != noErr)
- {
- # if ENABLE_DEBUG
- DisplayCString ("Cannot open prefs file.\r");
- # endif
- return;
- }
- if (GetEOF (vRefNum, &size) == noErr)
- {
- # if ENABLE_DEBUG
- DisplayCString ("size of prefs file: ");
- DisplayLong (size);
- DisplayLn ();
- # endif
- /* size must be at least enough for the version number at the front */
- if (size >= sizeof (short))
- {
- /*
- * Read the file into a generic block of memory. This strategy
- * is used in case the Prefs structure undergoes modification in
- * the future. By reading it into a generic block and examining
- * the version number, the block can be interpreted appropriately.
- */
-
- if ((h = NewHandle (size)) != (Handle) nil)
- {
- HLock (h);
- count = size;
- if (FSRead (vRefNum, &count, *h) == noErr)
- {
- if (count == size)
- InterpPreferences (h, size, prefs);
- }
- HUnlock (h);
- DisposeHandle (h);
- }
- }
- }
- (void) FSClose (vRefNum);
- }
-
-
- void
- WritePreferences (Prefs *prefs)
- {
- FSSpec fss;
- short vRefNum;
- long size;
- Handle h;
- OSErr err;
-
- if ((err = MakePrefsFileFSS (&fss)) != noErr)
- {
- # if ENABLE_DEBUG
- DisplayCString ("No Preferences file, err = ");
- DisplayLong (err);
- DisplayLn ();
- # endif
- if (err != fnfErr)
- return;
- # if ENABLE_DEBUG
- DisplayCString ("Creating prefs file.\r");
- # endif
- err = FSpCreate (&fss, creatorType, prefsFileType, smSystemScript);
- if (err != noErr)
- {
- # if ENABLE_DEBUG
- DisplayCString ("prefs file creation error: ");
- DisplayLong (err);
- DisplayLn ();
- # endif
- return;
- }
- }
- if (FSpOpenDF (&fss, fsWrPerm, &vRefNum) != noErr)
- {
- # if ENABLE_DEBUG
- DisplayCString ("Cannot open prefs file for writing.\r");
- # endif
- return;
- }
- size = sizeof (Prefs);
- FSWrite (vRefNum, &size, (Ptr) prefs);
- if (size != sizeof (Prefs))
- {
- # if ENABLE_DEBUG
- DisplayCString ("error writing prefs file.\r");
- # endif
- size = 0; /* clobber contents to prevent misinterpretation on next read */
- }
- (void) SetEOF (vRefNum, sizeof (Prefs));
- (void) FSClose (vRefNum);
- }